#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 21-3-25 下午3:43
# @Author  : zgd
# @Site    : 
# @File    : image_storage.py
# @Software: PyCharm
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag,put_data
import qiniu.config

#需要填写你的 Access Key 和 Secret Key
access_key = 'cR4nch9WqtKXb4u5ZibvxKJTpvh5Y9k4u2P_PYsj'
secret_key = 'iWA-l7WM8GEiUgzp3oiNSGIGKielLkdWI9QECufS'

def image_stroage(image_data):
    #构建鉴权对象
    q = Auth(access_key, secret_key)

    #要上传的空间
    bucket_name = 'info36-test'

    #上传后保存的文件名
    key = None

    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    #要上传文件的本地路径
    # localfile = './hugege.png'

    # ret, info = put_file(token, key, localfile)
    ret, info = put_data(token, key, image_data)

    # status_code: 200,
    # x_log: X - Log,
    # text_body: {"hash": "FmgyyqnvM6o0EN9cUpGOlPzRQgrT",
    # "key": "FmgyyqnvM6o0EN9cUpGOlPzRQgrT"}, _
    # ResponseInfo__response: <Response[200] >,
    # exception: None,
    # req_id: 48AAAAAlhiLDh28W

    # 处理上传结果
    if info.status_code==200:
        return ret.get("key")
    else:
        return None
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)

if __name__ == '__main__':
    with open("./hugege.png","rb") as f:
        image_stroage(f.read())























